package com.tanhua.locationmanager.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.domain.db.SurveyDetails;
import com.tanhua.dubbo.api.db.PaperApi;
import com.tanhua.locationmanager.util.MyFDfs;
import com.tanhua.vo.PageResult;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Service
public class PaperService {

    @Reference
    private PaperApi paperApi;

    private final MyFDfs myFDfs;

    public PaperService(MyFDfs myFDfs) {
        this.myFDfs = myFDfs;
    }

    public ResponseEntity<?> queryPaper(Integer page, Integer pageSize) {
        Page<SurveyDetails> iPage = paperApi.queryPaper(page,pageSize);
        PageResult pageResult = new PageResult((int)iPage.getCurrent(),(int)iPage.getSize(),(int)iPage.getTotal(),iPage.getRecords());
        return ResponseEntity.ok(pageResult);
    }

    public ResponseEntity<?> savePaper(SurveyDetails surveyDetails) {
        paperApi.savePaper(surveyDetails);
        return ResponseEntity.ok().build();
    }

    public ResponseEntity<?> uploadCover(MultipartFile coverMulti) throws IOException {
        String cover = myFDfs.upload(coverMulti);
        return ResponseEntity.ok(cover);
    }

    public ResponseEntity<?> queryPaperById(Integer paperId) {
        SurveyDetails surveyDetails = paperApi.queryPaperById(paperId);
        return ResponseEntity.ok(surveyDetails);
    }

    public ResponseEntity<?> update(SurveyDetails surveyDetails) {
        paperApi.update(surveyDetails);
        return ResponseEntity.ok().build();
    }

    public ResponseEntity<?> delete(Integer paperId) {
        paperApi.delete(paperId);
        return ResponseEntity.ok().build();
    }
}
